package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ProxySetThenGetOAD类
 * 实现代理设置后读取请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxySetThenGetOAD implements AxdrType {

    private byte[] dataCode = null;
    private TSA tsa = null;
    private Unsigned16 timeOut = null;
    private SubSeqOfProxyReads seqOfProxyReads = null;

    public ProxySetThenGetOAD() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(TSA tsa, Unsigned16 timeOut, SubSeqOfProxyReads seqOfProxyReads) {
        this.tsa = tsa;
        this.timeOut = timeOut;
        this.seqOfProxyReads = seqOfProxyReads;
    }

    public void setTsa(TSA tsa) {
        this.tsa = tsa;
    }

    public void setTimeOut(Unsigned16 timeOut) {
        this.timeOut = timeOut;
    }

    public void setSeqOfProxyReads(SubSeqOfProxyReads seqOfProxyReads) {
        this.seqOfProxyReads = seqOfProxyReads;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);

        this.timeOut = new Unsigned16();
        codeLength += this.timeOut.decode(input);

        this.seqOfProxyReads = new SubSeqOfProxyReads();
        codeLength += this.seqOfProxyReads.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.tsa != null && this.timeOut != null && this.seqOfProxyReads != null) {
                codeLength = 0;
                codeLength += this.seqOfProxyReads.encode(output);
                codeLength += this.timeOut.encode(output);
                codeLength += this.tsa.encode(output);
            } else {
                throw new RuntimeException("tsa || timeOut || seqOfProxyReads is null");
            }
        }

        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding ProxySetThenGetOAD", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.tsa != null && this.timeOut != null && this.seqOfProxyReads != null) {
            return "sequence: {" + "tsa: " + this.tsa + ", timeOut: " + this.timeOut + ", seqOfProxyReads: "
                    + this.seqOfProxyReads + "}";
        } else {
            return "tsa || timeOut || seqOfProxyReads is null";
        }
    }

    public TSA getTsa() {
        return tsa;
    }

    public Unsigned16 getTimeOut() {
        return timeOut;
    }

    public SubSeqOfProxyReads getSeqOfProxyReads() {
        return seqOfProxyReads;
    }
}